<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二维码生成器</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.4/qrcode.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #1e1e1e 0%, #2d2d2d 100%);
            color: #ffffff;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
            background: rgba(45, 45, 45, 0.8);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .title {
            font-size: 2.5em;
            font-weight: 300;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .subtitle {
            color: #888;
            font-size: 1.1em;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #e0e0e0;
        }

        .form-group textarea,
        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px 15px;
            background: rgba(255, 255, 255, 0.05);
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            color: #ffffff;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group textarea {
            resize: vertical;
            min-height: 80px;
        }

        .form-group textarea:focus,
        .form-group input:focus,
        .form-group select:focus {
            outline: none;
            border-color: #4ecdc4;
            box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.2);
        }

        .color-input-group {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .color-input {
            width: 60px !important;
            height: 40px;
            border-radius: 8px;
            border: none;
            cursor: pointer;
        }

        .color-text {
            flex: 1;
            background: rgba(255, 255, 255, 0.05);
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            padding: 8px 12px;
            color: #ffffff;
            font-family: 'Courier New', monospace;
        }

        .qr-display {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-align: center;
            margin: 30px 0;
            padding: 30px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 15px;
            border: 2px dashed rgba(255, 255, 255, 0.2);
            min-height: 200px;
        }

        #qr-canvas {
            max-width: 100%;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            display: block;
            margin: 0 auto;
        }

        .placeholder {
            color: #666;
            font-size: 1.1em;
            padding: 40px 20px;
        }

        .download-btn {
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-transform: uppercase;
            letter-spacing: 1px;
            box-shadow: 0 5px 15px rgba(78, 205, 196, 0.3);
        }

        .download-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(78, 205, 196, 0.4);
        }

        .download-btn:active {
            transform: translateY(0);
        }

        .download-btn:disabled {
            background: #666;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        select {
            cursor: pointer;
        }

        select option {
            background: #2d2d2d;
            color: #ffffff;
        }

        .error-message {
            color: #ff6b6b;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        @media (max-width: 480px) {
            .container {
                margin: 10px;
                padding: 20px;
            }
            
            .title {
                font-size: 2em;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 class="title">二维码生成器</h1>
            <p class="subtitle">生成并下载url或文本的QR代码，并自定义背景和前景颜色。</p>
        </div>

        <form id="qr-form">
            <div class="form-group">
                <label for="text-input">内容:</label>
                <textarea id="text-input" placeholder="https://sunxiaozi.cn" required></textarea>
                <div class="error-message" id="text-error">请输入要生成二维码的内容</div>
            </div>

            <!-- <div class="form-group">
                <label for="foreground-color">Foreground color:</label>
                <div class="color-input-group">
                    <input type="color" id="foreground-color" class="color-input" value="#000000">
                    <input type="text" id="foreground-text" class="color-text" value="#000000">
                </div>
            </div>

            <div class="form-group">
                <label for="background-color">Background color:</label>
                <div class="color-input-group">
                    <input type="color" id="background-color" class="color-input" value="#ffffff">
                    <input type="text" id="background-text" class="color-text" value="#ffffff">
                </div>
            </div> -->

            <div class="form-group">
                <label for="error-correction">比例:</label>
                <select id="error-correction">
                    <option value="L">Low (~7%)</option>
                    <option value="M" selected>Medium (~15%)</option>
                    <option value="Q">Quartile (~25%)</option>
                    <option value="H">High (~30%)</option>
                </select>
            </div>
        </form>

        <div class="qr-display">
            <div class="placeholder" id="placeholder">
                在上方输入内容以生成二维码
            </div>
            <canvas id="qr-canvas" style="display: none;"></canvas>
        </div>

        <div style="text-align: center;">
            <button class="download-btn" id="download-btn" disabled>下载二维码</button>
        </div>
    </div>

    <script>
        const textInput = document.getElementById('text-input');
        const foregroundColor = document.getElementById('foreground-color');
        const backgroundColor = document.getElementById('background-color');
        const foregroundText = document.getElementById('foreground-text');
        const backgroundText = document.getElementById('background-text');
        const errorCorrection = document.getElementById('error-correction');
        const qrCanvas = document.getElementById('qr-canvas');
        const placeholder = document.getElementById('placeholder');
        const downloadBtn = document.getElementById('download-btn');

        // 更新颜色文本框
        function updateColorText() {
            foregroundText.value = foregroundColor.value;
            backgroundText.value = backgroundColor.value;
        }

        // 生成二维码
        function generateQRCode() {
            const text = textInput.value.trim();
            
            if (!text) {
                placeholder.style.display = 'block';
                qrCanvas.style.display = 'none';
                downloadBtn.disabled = true;
                return;
            }

            try {
                // 获取错误纠正级别映射 (正确的映射)
                const errorLevelMap = {
                    'L': 'L', // ~7%
                    'M': 'M', // ~15%
                    'Q': 'Q', // ~25%
                    'H': 'H'  // ~30%
                };

                const correctionLevel = errorLevelMap[errorCorrection.value] || 'M';
                
                // 动态确定合适的typeNumber
                let typeNumber = 1;
                let qr;
                
                // 尝试不同的typeNumber直到找到合适的
                for (let i = 1; i <= 40; i++) {
                    try {
                        qr = qrcode(i, correctionLevel);
                        qr.addData(text);
                        qr.make();
                        typeNumber = i;
                        break;
                    } catch (e) {
                        if (i === 40) throw e; // 如果最大尺寸还不行就抛出错误
                        continue;
                    }
                }

                // 获取模块数据
                const moduleCount = qr.getModuleCount();
                const cellSize = 300 / moduleCount;
                
                // 设置canvas
                qrCanvas.width = 300;
                qrCanvas.height = 300;
                const ctx = qrCanvas.getContext('2d');
                
                // 清除画布
                ctx.clearRect(0, 0, 300, 300);
                
                // 设置背景色
                ctx.fillStyle = backgroundColor.value;
                ctx.fillRect(0, 0, 300, 300);
                
                // 设置前景色并绘制QR码
                ctx.fillStyle = foregroundColor.value;
                for (let row = 0; row < moduleCount; row++) {
                    for (let col = 0; col < moduleCount; col++) {
                        if (qr.isDark(row, col)) {
                            ctx.fillRect(col * cellSize, row * cellSize, cellSize, cellSize);
                        }
                    }
                }

                placeholder.style.display = 'none';
                qrCanvas.style.display = 'block';
                downloadBtn.disabled = false;
                
            } catch (error) {
                console.error('QR生成错误:', error);
                placeholder.textContent = '生成二维码时出错，请尝试缩短文本内容';
                placeholder.style.display = 'block';
                qrCanvas.style.display = 'none';
                downloadBtn.disabled = true;
            }
        }

        // 下载二维码
        function downloadQRCode() {
            if (qrCanvas.style.display === 'none') return;
            
            const link = document.createElement('a');
            link.download = 'qr-code.png';
            link.href = qrCanvas.toDataURL();
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }

        // 事件监听器
        textInput.addEventListener('input', generateQRCode);
        foregroundColor.addEventListener('change', () => {
            updateColorText();
            generateQRCode();
        });
        foregroundColor.addEventListener('input', () => {
            updateColorText();
            generateQRCode();
        });
        backgroundColor.addEventListener('change', () => {
            updateColorText();
            generateQRCode();
        });
        backgroundColor.addEventListener('input', () => {
            updateColorText();
            generateQRCode();
        });
        errorCorrection.addEventListener('change', generateQRCode);
        downloadBtn.addEventListener('click', downloadQRCode);

        // 初始化
        updateColorText();
        
        // 添加默认文本用于测试
        textInput.value = 'https://it-tools.tech';
        
        // 延迟生成以确保库加载完成
        setTimeout(() => {
            generateQRCode();
        }, 100);

        // 添加颜色文本框的手动编辑功能
        foregroundText.addEventListener('input', function() {
            const color = this.value.trim();
            if (/^#[0-9A-Fa-f]{6}$/.test(color)) {
                foregroundColor.value = color;
                generateQRCode();
            }
        });

        backgroundText.addEventListener('input', function() {
            const color = this.value.trim();
            if (/^#[0-9A-Fa-f]{6}$/.test(color)) {
                backgroundColor.value = color;
                generateQRCode();
            }
        });

        // 添加输入验证动画
        textInput.addEventListener('focus', function() {
            this.style.borderColor = '#4ecdc4';
        });
        
        textInput.addEventListener('blur', function() {
            if (!this.value.trim()) {
                this.style.borderColor = 'rgba(255, 255, 255, 0.1)';
            }
        });
    </script>
</body>
</html>